package net.orange.architecture.validator;

import java.util.ArrayList;
import java.util.Collection;

/**
 * The validator which can perform some specify logic validations.
 */
public class Validator {
 
	private Collection<IObjectValidator> validators = new ArrayList<IObjectValidator>();

	private Collection<String> msgs;
	/**
	 * Disabled the default constructor.
	 */
	@SuppressWarnings("unused")
	private Validator(){
		
	}
	/**
	 * Constructor with the message array.
	 * @param msgs The message array.
	 */
	public Validator(Collection<String> msgs) {
		this.msgs = msgs;
	}
	/**
	 * Add the object validator.
	 * @param ov The object validator.
	 */
	public void add(IObjectValidator ov){
		validators.add(ov);
	}
	/**
	 * Remove the object validator.
	 * @param ov
	 */
	public void remove(IObjectValidator ov){
		validators.remove(ov);
	}	
	/**
	 * Clear all the object validator.
	 */
	public void clear(){
		validators.clear();
	}	
	/**
	 * Check the validatity
	 * @return null or empty for pass the validation,the error message otherwise. 
	 */
	public void validate() throws Exception {
		if (validators == null || validators.iterator() == null)
			return;

		for(IObjectValidator ov : validators){
			String msg = ov.validate();
			if (msg != null && msg.length() > 0){
				msgs.add(msg);
				return;
			}			
		}
	}
}
